package com.nobuyme.box.toy.manage;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.nobuyme.box.R;
import com.nobuyme.box.account.Account;
import com.nobuyme.box.account.AccountManager;
import com.nobuyme.box.app.UrlCenter;
import com.nobuyme.box.toy.ToyListFragment;
import com.nobuyme.box.toy.ToyManageListActivity;
import com.nobuyme.box.util.UICommon;
import com.nobuyme.box.xunfei.XunFeiSpeech;
import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.pachong.android.framework.httprequest.HttpRequest;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;
import com.pachong.android.frameworkbase.utils.EasyToast;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import android_serialport_api.SerialPortFinder;
import butterknife.Bind;
import butterknife.OnClick;


public class NeedReplaceActivity extends BaseActivity {
    @Bind(R.id.tv_need_replace_state)
    TextView mTvNeedReplaceState;
    @Bind(R.id.iv_place_goods)
    ImageView mIvPlaceGoods;
    @Bind(R.id.tv_place_goods)
    TextView mTvPlaceGoods;
    @Bind(R.id.tv_place_id)
    TextView mTvPlaceId;
    int position;
    @Bind(R.id.rl_error_back)
    RelativeLayout mRlErrorBack;
    private ToyListFragment.TestBean data;
    private SerialPortFinder mSerialPortFinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hideToolbar();
        data = (ToyListFragment.TestBean) getIntent().getSerializableExtra("data");
        position = getIntent().getIntExtra("position", -1);
        initData();
    }

    private void initData() {
        mSerialPortFinder = UICommon.getInstance().getSerialPortFinder();
        mTvNeedReplaceState.setText(position + "号储物格已打开");
        Glide.with(this).load(data.getCover()).into(mIvPlaceGoods);
        mTvPlaceGoods.setText(data.getName());
        mTvPlaceId.setText("商品ID:" + data.getGoods_id());
        try {
            XunFeiSpeech xunFeiSpeech = new XunFeiSpeech(this);
            xunFeiSpeech.speak("12", position + "号储物格已打开，取出商品后请关闭柜门", new XunFeiSpeech.OnCompleteListener() {
                @Override
                public void onComplete() {
//                    EasyToast.showToast(PutGoodsActivity.this,"柜门已打开");
                }
            });
        } catch (NullPointerException e) {
        }
        //万科柜子、串口1的 17打不开。18打开的是17     19打开18
        Account acc = AccountManager.getCurrentAccount();
        //需归还开柜
        PutGoodsActivity.openLattice(acc.getId(), acc.getPolling_one(), acc.getPolling_two(), acc.getPolling_three(), acc.getPolling_four(), position);
//        if (acc.getId() == 2) {
//            if (data.getLattice_id() >= 17 || data.getLattice_id() <= 20) {
//                UICommon.getInstance().writeData(DriveControlLattice.getInstance().LatticeOnOff(2 + "", data.getLattice_id() + 1));
//            } else {
//                UICommon.getInstance().writeData(DriveControlLattice.getInstance().LatticeOnOff(2 + "", data.getLattice_id()));
//            }
//        } else {
//            UICommon.getInstance().writeData(DriveControlLattice.getInstance().LatticeOnOff(2 + "", data.getLattice_id()));
//        }
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_need_replace, null);
    }

    public static void start(Activity activity, ToyListFragment.TestBean data, int position) {
        Intent intent = new Intent(activity, NeedReplaceActivity.class);
        intent.putExtra("data", data);
        intent.putExtra("position", position);
        activity.startActivity(intent);
    }

    @OnClick({R.id.btn_need_replace_complete,
            R.id.btn_need_replace_cancel,
            R.id.btn_need_replace_error,
            R.id.btn_toy_error,
            R.id.btn_toy_good,
            R.id.btn_toy_bad,
            R.id.btn_toy_null
    })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_need_replace_complete:
//                finish();
                setStateId();
                break;
            case R.id.btn_need_replace_cancel:
                ToyManageListActivity.start(NeedReplaceActivity.this,2);
                finish();
                break;
            case R.id.btn_need_replace_error:
                mRlErrorBack.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_toy_good:
                //玩具正常的异常回收
                try {
                    String strUT = URLEncoder.encode(getString(R.string.put_replace_toy_good), "UTF-8");
                    errorBack(strUT);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_toy_bad:
                //玩具损坏的异常回收
                try {
                    String strUT = URLEncoder.encode(getString(R.string.put_replace_toy_bad), "UTF-8");
                    errorBack(strUT);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_toy_null:
                //玩具为空的异常回收
                try {
                    String strUT = URLEncoder.encode(getString(R.string.put_replace_toy_null), "UTF-8");
                    errorBack(strUT);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                break;
            //点错了
            case R.id.btn_toy_error:
                mRlErrorBack.setVisibility(View.GONE);
                break;
        }
    }

    private void setStateId() {
        HttpRequest request = new HttpRequest(this);
        String s = String.format(UrlCenter.PUT_GOODS, data.getLattice_id());
        String urlPut = s + "?type=0";
        request.put(urlPut, null, new DataRequestListener<Object>(Object.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
//                EasyToast.showToast(NeedReplaceActivity.this, "回收成功");
                ToyManageListActivity.start(NeedReplaceActivity.this,2);
                finish();
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    //异常回收
    private void errorBack(String content){
        HttpRequest request = new HttpRequest(this);
        String s = String.format(UrlCenter.PUT_GOODS, data.getLattice_id());
        String urlPut = s + "?type=2&remark="+content;
        request.put(urlPut, null, new DataRequestListener<Object>(Object.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
                ToyManageListActivity.start(NeedReplaceActivity.this,2);
                finish();
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                EasyToast.showToast(NeedReplaceActivity.this, strMsg);
            }
        });
    }
}
